/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
package awd.bj.jls.webs.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

import awd.bj.jls.model.WpglModel;
import awd.bj.jls.model.WpjsModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;
import awd.bj.jls.webs.utils.StringUtils;

@Controller
@RequestMapping("/wpjs")
@RefreshScope
public class WpjsAction extends BaseAction {

    @Autowired
    private JlsServersApi jlsServersApi;

    @RequestMapping({"/wpjs.html" //首页
            ,"/wpjs_enter.html" //单表录入页面
            ,"/wpjs_jbxx_enter.html" //人员信息查询页面
            ,"/wpjs_jbxx_sw_enter.html" //人员信息查询页面
            ,"/wpjs_query.html" //信息查询
            ,"/wpjs_jbxx_query.html" //关联基本信息查询
            ,"/6.html" //
            ,"/7.html"})
    public String index(Model model, HttpServletRequest request) {
        model.addAllAttributes(getBaseModelData());
        return getRenderFilePath(request);
    }

    /**
     * 数据列表,关联基本信息
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list2jbxx")
    @ResponseBody
    public Map<String, Object> list2jbxx(HttpServletRequest request,Map<String,Object> params) {
        QueryParam param = getBaseQueryParam(request,params);
        System.err.println("=======================何磊=========================");
        System.err.println("======================查询数据======================");
        System.err.println(JSON.toJSONString(param));
        System.err.println("===========================何磊===========================");
        ResponseMessage<PagerResult<WpjsModel>> list = jlsServersApi.wpjsQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }

    /**
     * 数据列表
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request,Map<String,Object> params) {
        QueryParam param = getBaseQueryParam(request,params);
        System.err.println("=======================何磊=========================");
        System.err.println("======================查询数据======================");
        System.err.println(JSON.toJSONString(param));
        System.err.println("===========================何磊===========================");
        ResponseMessage<PagerResult<WpjsModel>> list = jlsServersApi.wpjsQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }

    /**
     * 数据保存操作
     * @param
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result<?> save(HttpServletRequest request, @ModelAttribute WpjsModel wpjsmodel){
        User users = getCurrentUser();
        if(StringUtils.isNullOrEmpty(wpjsmodel.getRybh())){
            return ResultUtils.error("人员编号未传！");
        }
        String taskid = request.getParameter("taskid");
        String ywlcid = request.getParameter("ywlcid");
        System.err.println("=====================送物登记-物品接收======================");
        System.err.println(JSON.toJSONString(wpjsmodel));
        System.err.println("========================================================");
        jlsServersApi.wpjsSave(ywlcid,taskid,wpjsmodel);
        WpglModel model = new WpglModel();
        try {
            model.setState("R2");
            model.setJsbh(users.getJsbh());
            model.setCreator(users.getUsername());
            model.setCreatetime(new Date());
            return ResultUtils.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }


    /**
     * 数据保存操作
     * @param
     * @return
     */
    @RequestMapping("/wpglSaveByZrs")
    @ResponseBody
    public Result<?> wpglSaveByZrs(String jsonList,HttpServletRequest request){
        User users = getCurrentUser();
        try {
        	 String taskid = request.getParameter("taskid");
             String ywlcid = request.getParameter("ywlcid");
            List<WpglModel> list = JSON.parseArray(jsonList,WpglModel.class);
            System.err.println("何磊:保存数据============================================");
            list.forEach(model->{
                System.err.println("model=========="+JSON.toJSONString(model));
            });

            List<WpglModel> listwpgl = new ArrayList<>();
            for (int i=0 ; i< list.size(); i++){
            	System.err.println(list.size());
                if(!StringUtils.isNullOrEmpty(list.get(i).getMc())) {
                	list.get(i).setState("R2");
                	list.get(i).setJsbh(users.getJsbh());
                	list.get(i).setLqzt("0");
                	list.get(i).setCreatetime(new Date());
                	list.get(i).setCreator(users.getUsername());
                	listwpgl.add(list.get(i));
                }
            }
            System.err.println("-----"+JSON.toJSON(listwpgl)+"---"+taskid+"---"+ywlcid);
            return ResultUtils.success(jlsServersApi.wpglSaveByZcrs(taskid, ywlcid, listwpgl));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }



    /**
     * 数据更新
     * @param model
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result<?> update(@ModelAttribute WpjsModel model) {
        User users = getCurrentUser();
        try {
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            return ResultUtils.success(jlsServersApi.wpjsUpdate(model.getId(),model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 删除操作
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result<?> delete(@ModelAttribute WpjsModel model) {
        User users = getCurrentUser();
        try {
            model.setState("R3");
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            return ResultUtils.success(jlsServersApi.wpjsUpdate(model.getId(),model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "删除失败");
        }
    }

//    /**
//     * 物品接收保存
//     * @param model
//     * @return
//     */
//    @RequestMapping("/wpjsSave")
//    @ResponseBody
//    public Result<?> wpjsSave(HttpServletRequest request,@ModelAttribute WpjsModel model){
//        User users = getCurrentUser();
//        try {
//            model.setState("R2");
//            model.setJsbh(users.getJsbh());
//            model.setCreator(users.getUsername());
//            model.setCreatetime(new Date());
//            return ResultUtils.success(jlsServersApi.wpjsSave(model));
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
//        }
//    }

    /**
     * 接收（代收）
     * @param model
     * @return
     */
    @RequestMapping("/jsSave")
    @ResponseBody
    public Result<?> jsSave(HttpServletRequest request, @ModelAttribute WpjsModel model) {
        User users = getCurrentUser();
        if(StringUtils.isNullOrEmpty(model.getRybh())){
            return ResultUtils.error("人员编号未传！");
        }
        String taskid = request.getParameter("taskid");
        if(StringUtils.isNullOrEmpty(taskid)){
            return ResultUtils.error("taskid未传！");
        }
        try {
            model.setJsbh(users.getJsbh());
            ResponseMessage<String> responseMessage = jlsServersApi.jsSave(taskid, model);
            return ResultUtils.success(responseMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 派发物品
     * @param model
     * @return
     */
    @RequestMapping("/pfwpSave")
    @ResponseBody
    public Result<?> pfwpSave(HttpServletRequest request, @ModelAttribute WpjsModel model) {
        User users = getCurrentUser();
        if(StringUtils.isNullOrEmpty(model.getRybh())){
            return ResultUtils.error("人员编号未传！");
        }
        String taskid = request.getParameter("taskid");
        try {
            model.setJsbh(users.getJsbh());
            ResponseMessage<String> responseMessage = jlsServersApi.pfwpSave(taskid, model);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }





}
